package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Major;
import com.pojo.Majortype;
import com.pojo.Module;
import com.service.MajorService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/major")
public class MajorAction {

    @Autowired
    private MajorService majorService ;

    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;

    /**
     * 分页查询专业信息
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/pages")
    public ResponseBean majors(
            @RequestParam(defaultValue = "1") Integer page ,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) String key,
            @RequestParam(required = false) String mtid){
        IPage iPage = majorService.majors(page, limit,key,mtid) ;
        return new ResponseBean(0 , iPage.getTotal() , "ok" , iPage.getRecords()) ;
    }

    /**
     * 根据id查询专业
     * @return
     */
    @RequestMapping("/getMajorById")
    public ResponseBean getMajorById(String mjid){
        Major major = majorService.getById(mjid);
        if (major != null){
            return new ResponseBean(200,"查询成功",major);
        }
        return ResponseBean.error("查询失败");
    }

    /**
     *修改专业信息
     * @return
     */
    @RequestMapping("/updateMajor")
    public ResponseBean updateMajor(@RequestBody Major major){
        if(StringUtil.isNull(major.getMjid())) {
            return ResponseBean.error("专业id不能为空") ;
        }
        if(StringUtil.isNull(major.getMjimg())) {
            major.setMjid(null);
        }
        if(StringUtil.isNull(major.getMjname())){
            major.setMjname(null);
        }
        Major majorTwo = majorService.getById(major.getMjid());
        if(majorService.updateById(major)){
            if (!major.getMjimg().equals(majorTwo.getMjimg())){
                FileUtil.copy(tmp + major.getMjimg(), upload, true) ;
            }
            return ResponseBean.ok("操作成功");
        }
        return  ResponseBean.error("操作失败");
    }

    /**
     * 添加专业
     */
    @RequestMapping("/addMajor")
    public ResponseBean addMajor(@RequestBody Major major){
        if(StringUtil.isNull(major.getMjimg())) {
            return ResponseBean.error("请先上传图片") ;
        }
        if(StringUtil.isNull(major.getMjname())) {
            return ResponseBean.error("请填写专业名称") ;
        }
        if(StringUtil.isNull(major.getMtid())) {
            return ResponseBean.error("请选择专业类型") ;
        }

        Major m = majorService.getOne(new QueryWrapper<Major>().eq("mjname",major.getMjname()).last("limit 1"));
        if(m != null){
            return ResponseBean.error("名字已经存在");
        }
        boolean res = majorService.save(major) ;
        FileUtil.copy(tmp + major.getMjimg(), upload, true) ;
        return res ? new ResponseBean(200 , "成功" , "") : ResponseBean.error("新增失败") ;
    }

    /**
     * 根据id删除专业
     * @param mjid
     * @return
     */
    @RequestMapping("/deleteMajor")
    public ResponseBean deleteMajor(String mjid){
        if(majorService.removeById(mjid)){
            return  new ResponseBean(0,1,"删除成功");
        }else{
            return  new ResponseBean(1,1,"删除失败");
        }
    }

}

